﻿using System;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using VioletDevelopmentLaboratory.Net.Web.Configuration;
using VioletDevelopmentLaboratory.Net.Web.Component;

namespace VioletDevelopmentLaboratory.Net.Web.WebControls
{
	public class CommentSummaryTemplateContainer : Control, INamingContainer
	{
		private BoardComment currentComment;
		public BoardComment CurrentComment { get { return this.currentComment; } }

		internal CommentSummaryTemplateContainer(BoardComment currentComment)
		{
			this.currentComment = currentComment;
		}
	}

	public class CommentSummaryRowDataBoundEventArgs : EventArgs
	{
		public CommentSummaryRowDataBoundEventArgs(CommentSummaryTemplateContainer summaryTemplate)
		{
			this.row = summaryTemplate;
		}

		private CommentSummaryTemplateContainer row;
		public CommentSummaryTemplateContainer Row { get { return this.row; } }
	}

	public delegate void CommentSummaryRowDataBoundEventHandler(Object sender, CommentSummaryRowDataBoundEventArgs e);

	[ToolboxData("<{0}:BoardCommentSummary runat=server></{0}:BoardCommentSummary>"),
	ParseChildren(ChildrenAsProperties = true),
	Designer(typeof(VWebControlDesigner))]
	public class BoardCommentSummary : BoardSummaryBase
	{
		#region Stress
		[Browsable(true),
		Category("Appearance"),
		DefaultValue(""),
		Editor(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor)),
		UrlProperty]
		public String NewImageUrl { get { return ViewState["NewImageUrl"].ToString(); } set { ViewState["NewImageUrl"] = value; } }

		[Browsable(true),
		Category("Appearance"),
		DefaultValue("New")]
		public String NewImageAlternateText { get { return ViewState["NewImageAlternateText"].ToString(); } set { ViewState["NewImageAlternateText"] = value; } }

		[Browsable(true),
		Category("Appearance"),
		DefaultValue(""),
		Editor(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor)),
		UrlProperty]
		public String TodayImageUrl { get { return ViewState["TodayImageUrl"].ToString(); } set { ViewState["TodayImageUrl"] = value; } }

		[Browsable(true),
		Category("Appearance"),
		DefaultValue("Today")]
		public String TodayImageAlternateText { get { return ViewState["TodayImageAlternateText"].ToString(); } set { ViewState["TodayImageAlternateText"] = value; } }
		#endregion

		#region Template
		private ITemplate itemTemplate;
		[TemplateContainer(typeof(CommentSummaryTemplateContainer)),
		PersistenceMode(PersistenceMode.InnerProperty),
		Browsable(false)]
		public ITemplate ItemTemplate { get { return this.itemTemplate; } set { this.itemTemplate = value; } }

		private ITemplate headerTemplate;
		[TemplateContainer(typeof(CommentSummaryTemplateContainer)),
		PersistenceMode(PersistenceMode.InnerDefaultProperty),
		Browsable(false)]
		public ITemplate HeaderTemplate { get { return this.headerTemplate; } set { this.headerTemplate = value; } }

		private ITemplate footerTemplate;
		[TemplateContainer(typeof(CommentSummaryTemplateContainer)),
		PersistenceMode(PersistenceMode.InnerDefaultProperty),
		Browsable(false)]
		public ITemplate FooterTemplate { get { return this.footerTemplate; } set { this.footerTemplate = value; } }
		#endregion

		#region Event
		[Category("Action")]
		public event CommentSummaryRowDataBoundEventHandler RowDataBound;
		#endregion

		public BoardCommentSummary() : base()
		{
			this.NewImageUrl = "";
			this.NewImageAlternateText = "";
			this.TodayImageUrl = "";
			this.TodayImageAlternateText = "";
		}

		protected override Control createHeader()
		{
			if (this.HeaderTemplate == null)
			{
				Literal header = new Literal();

				if (String.IsNullOrEmpty(this.HeaderImageUrl))
				{
					header.Text = String.Format("<div class=\"Header\">{0}</div>",
						this.HeaderText);
				}
				else
				{
					header.Text = String.Format("<div class=\"Header\"><img src=\"{0}\" alt=\"{1}\" /></div>",
						ResolveClientUrl(this.HeaderImageUrl),
						this.HeaderText);
				}

				return header;
			}
			else
			{
				CommentSummaryTemplateContainer template = null;

				template = new CommentSummaryTemplateContainer(null);
				template.EnableViewState = false;
				this.HeaderTemplate.InstantiateIn(template);

				return template;
			}
		}

		protected override Control createList()
		{
			BoardCommentCollection boardCommentCollection = BoardComment.GetLastCommentCollection(this.PageSize);

			if (boardCommentCollection != null)
			{
				if (this.ItemTemplate != null)
				{
					#region Template
					Control items = new Control();
					items.EnableViewState = false;

					CommentSummaryTemplateContainer template = null;
					CommentSummaryRowDataBoundEventArgs args = null;

					foreach (BoardComment comment in boardCommentCollection)
					{
						template = new CommentSummaryTemplateContainer(comment);
						template.EnableViewState = false;
						this.ItemTemplate.InstantiateIn(template);
						template.DataBind();

						args = new CommentSummaryRowDataBoundEventArgs(template);
						if (this.RowDataBound != null)
						{
							this.RowDataBound(this, args);
						}

						items.Controls.Add(template);
					}

					return items;
					#endregion
				}
				else
				{
					Literal items = new Literal();
					items.Text = "<ul>";

					#region Default Template
					String liStringFormat = "<li>{0}{1}{2}</li>";

					foreach (BoardComment comment in boardCommentCollection)
					{
						#region Link
						String link = "<a href=\"{0}\">{1}</a>";
						String linkUrl = "";
						String linkText = "";
						if (!String.IsNullOrEmpty(comment.PageUrl))
							linkUrl = comment.PageUrl.Replace("&", "&amp;");

						String content = this.removeHtmlTags(comment.Content).Trim();

						if (content.Length > this.ItemLength)
							linkText +=  Page.Server.HtmlEncode(Page.Server.HtmlDecode(content).Substring(0, this.ItemLength).Trim() + this.ReduceCharacter);
						else
							linkText += content;

						link = String.Format(link, linkUrl, linkText);
						#endregion

						#region Stress
						String image = "";
						String imageUrl = "";
						String imageText = "";
						//새글강조
						DateTime _now = DateTime.Now;
						TimeSpan _timeSpan = _now - comment.RegistrationDate;
						if (_timeSpan.TotalMinutes < 1440)
						{
							image = "&nbsp;<img src=\"{0}\" alt=\"{1}\" />";
							imageText = this.NewImageAlternateText;

							if (!String.IsNullOrEmpty(this.NewImageUrl))
								imageUrl = ResolveClientUrl(this.NewImageUrl);

							image = String.Format(image, imageUrl, imageText);
						}

						//오늘글강조
						if (DateTime.Now.ToShortDateString() == comment.RegistrationDate.ToShortDateString())
						{
							image = "&nbsp;<img src=\"{0}\" alt=\"{1}\" />";
							imageText = this.TodayImageAlternateText;

							if (!String.IsNullOrEmpty(this.TodayImageUrl))
								imageUrl = ResolveClientUrl(this.TodayImageUrl);

							image = String.Format(image, imageUrl, imageText);
						}
						#endregion

						items.Text += String.Format(liStringFormat,
							String.Format(this.DateFormat, comment.RegistrationDate),
							link,
							image);
					}
					items.Text += "</ul>";
					#endregion

					return items;
				}
			}

			Literal emptyControl = new Literal();
			emptyControl.EnableViewState = false;
			emptyControl.Text = String.Format("<div class=\"Empty\">{0}</div>", this.EmptyDataText);

			return emptyControl;
		}

		protected override Control createFooter()
		{
			if (this.FooterTemplate == null)
			{
				Literal div = new Literal();

				if (String.IsNullOrEmpty(this.FooterImageUrl))
				{
					div.Text = String.Format("<div class=\"Footer\">{0}</div>", this.FooterText);
				}
				else
				{
					div.Text = String.Format("<div class=\"Footer\"><img src=\"{0}\" alt=\"{1}\" /></div>",
						ResolveClientUrl(this.FooterImageUrl),
						this.FooterText);
				}

				return div;
			}
			else
			{
				CommentSummaryTemplateContainer template = null;

				template = new CommentSummaryTemplateContainer(null);
				template.EnableViewState = false;
				this.FooterTemplate.InstantiateIn(template);

				return template;
			}
		}

		protected override void CreateChildControls()
		{
			this.PageSize = VUtility.GetControlSetting(1, 100, this.PageSize, "BoardCommentSummary.PageSize", 5);
			this.ItemLength = VUtility.GetControlSetting(1, 100, this.ItemLength, "BoardCommentSummary.ItemLength", 15);
			this.DateFormat = VUtility.GetControlSetting(this.DateFormat, "BoardCommentSummary.DateFormat", "{0:d}&nbsp;");
			this.ReduceCharacter = VUtility.GetControlSetting(this.ReduceCharacter, "BoardCommentSummary.ReduceCharacter", "...");
			this.EmptyDataText = VUtility.GetControlSetting(this.EmptyDataText, "BoardCommentSummary.EmptyDataText", "No Data.");

			this.CssClass = VUtility.GetControlSetting(this.CssClass, "BoardCommentSummary.CssClass", "BoardCommentSummray_Default");

			this.HeaderText = VUtility.GetControlSetting(this.HeaderText, "BoardCommentSummary.HeaderText", "Last Comments");
			this.HeaderImageUrl = VUtility.GetControlSetting(this.HeaderImageUrl, "BoardCommentSummary.HeaderImageUrl", "");

			this.FooterText = VUtility.GetControlSetting(this.FooterText, "BoardCommentSummary.FooterText", "");
			this.FooterImageUrl = VUtility.GetControlSetting(this.FooterImageUrl, "BoardCommentSummary.FooterImageUrl", "");

			this.NewImageUrl = VUtility.GetControlSetting(this.NewImageUrl, "BoardCommentSummary.NewImageUrl",
				Page.ClientScript.GetWebResourceUrl(this.GetType(), "VioletDevelopmentLaboratory.Net.Web.WebResources.stress_new_small.gif").Replace("&", "&amp;"));
			this.NewImageAlternateText = VUtility.GetControlSetting(this.NewImageAlternateText, "BoardCommentSummary.NewImageAlternateText", "New");

			this.TodayImageUrl = VUtility.GetControlSetting(this.TodayImageUrl, "BoardCommentSummary.TodayImageUrl",
				Page.ClientScript.GetWebResourceUrl(this.GetType(), "VioletDevelopmentLaboratory.Net.Web.WebResources.stress_today_small.gif").Replace("&", "&amp;"));
			this.TodayImageAlternateText = VUtility.GetControlSetting(this.TodayImageAlternateText, "BoardCommentSummary.TodayImageAlternateText", "Today");

			base.CreateChildControls();
		}
	}
}
